﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace RenameFoldersAndFiles
{
    class Program
    {
        static void Main(string[] args)
        {
            var path = $@"\\tdtdown\MapTileDownload\arcgis\stest";
            if (args.Length >= 1)
            {
                path = args[0];
            }




            DirectoryInfo directoryInfo = new DirectoryInfo(path);
            #region 更改文件夹名称
            //var subDirectories = directoryInfo.GetDirectories("*", SearchOption.AllDirectories);
            //subDirectories.ToList().ForEach((subDirectory) =>
            //{

            //    var intOfZ = -1;
            //    var intOfY = -1;
            //    var relativePath = subDirectory.FullName.Replace(directoryInfo.FullName, "");
            //    var split = relativePath.Split('\\');
            //    ;
            //    try
            //    {
            //        intOfZ = int.Parse(split[1]);
            //    }
            //    catch (Exception)
            //    {
            //        var replacedCappedString = split[1].ToUpper().Replace("L", "");
            //        try
            //        {
            //            intOfZ = int.Parse(replacedCappedString);
            //        }
            //        catch (Exception)
            //        {
            //        }
            //    }
            //    if (split.Length >= 3)
            //    {
            //        try
            //        {
            //            intOfY = int.Parse(split[2]);
            //        }
            //        catch (Exception)
            //        {
            //            var replacedCappedString = split[2].ToUpper().Replace("L", "");
            //            try
            //            {
            //                intOfY = int.Parse(replacedCappedString);
            //            }
            //            catch (Exception)
            //            {
            //            }
            //        }
            //    }
            //    var newRelativeName = string.Empty;
            //    if (intOfY >= 0)
            //    {
            //        //R0000000b
            //        newRelativeName = $@"L{intOfZ:D2}\R{intOfY:X8}";
            //    }
            //    else
            //    {
            //        newRelativeName = $@"L{intOfZ:D2}";
            //    }
            //    var newFullName = Path.Combine(directoryInfo.FullName, newRelativeName);
            //    var oldFullName = subDirectory.FullName;
            //    if (newFullName != oldFullName)
            //    {
            //        Directory.Move(sourceDirName: oldFullName, destDirName: newFullName);
            //    }
            //    ;
            //    //var name = subDirectory.Name;
            //    //int intOfName = -1;
            //    //if (intOfName > 0)
            //    //{
            //    //    var newName = $"L{intOfName:D2}";
            //    //    if (newName.ToUpper() != name.ToUpper())
            //    //    {
            //    //        //Directory.Move()
            //    //    }
            //    //    ;
            //    //}
            //});
            #endregion

            #region 更改文件名
            var pngFiles = directoryInfo.GetFiles("*", SearchOption.AllDirectories);
            pngFiles.ToList().ForEach((pngfile) =>
            {

                var intOfZ = -1;
                var intOfY = -1;
                var intOfX = -1;
                var relativePath = pngfile.FullName.Replace(directoryInfo.FullName, "");
                var split = relativePath.Split('\\');
                ;
                try
                {
                    intOfX = int.Parse(split[3]);
                }
                catch (Exception)
                {
                    try
                    {
                        split[3].ToUpper();

                        //intOfX = int.Parse(split[3].ToUpper().Replace("C", string.Empty));
                    }
                    catch (Exception)
                    {
                        throw;
                    }
                }



                if (split.Length >= 3)
                {
                    try
                    {
                        intOfY = int.Parse(split[2]);
                    }
                    catch (Exception)
                    {
                        var replacedCappedString = split[2].ToUpper().Replace("L", "");
                        try
                        {
                            intOfY = int.Parse(replacedCappedString);
                        }
                        catch (Exception)
                        {
                        }
                    }
                }

            });
            #endregion

        }
    }
}
